home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-25 | 1022 b | 41 lines | [TEXT/CWIE] |
- // STL1.cp
- #include <iostream>
- #include <cstddef>
- #include <cassert>
-
- int ANSICqsortCompare(const void *el1, const void *el2)
- {
- return (int)(*(long *)el1 - *(long *)el2);
- }
-
- int main()
- {
- std::cout << "STL sort sample!" << std::endl << std::endl; // setting up arrays
- const std::size_t aSize = 102400;
- long *qArray = new long[aSize];
- long *stlArray = new long[aSize];
- assert(qArray && stlArray);
-
- std::srand(std::time(NULL)); // randomize
- for (int i = 0; i < aSize; ++i)
- {
- qArray[i] = stlArray[i] = std::rand();
- }
-
- long qsortTickStart = std::clock(); // ANSI sort
- std::qsort(qArray, aSize, sizeof(long), ANSICqsortCompare);
- long qTix = std::clock() - qsortTickStart;
-
- long stlTickStart = std::clock(); // STL sort
- std::sort(stlArray, &stlArray[aSize]);
- long stlTix = std::clock() - stlTickStart;
-
- for (int i = 0; i < aSize; ++i) // verify
- {
- assert(qArray[i] == stlArray[i]);
- }
- std::cout << "qsort() " << qTix << "\nSTL sort " << stlTix << "\n";
- delete [] qArray; delete [] stlArray;
- }
-
-